<?php
namespace Libs\Common;
/*
 * Class Billing Android
 * @author ThamLT
 */
class BillingAndroidCommon
{
    /**
     * Check subscription purchase status
     * @author ThamLT
     * */
     public static function isValidSubscription($productId, $purChaseToken)
     {
        /*    define('GOOGLE_WALLET_PACKAGENAME', 'com.my.firstapp');
            $subscriptionId = '12999763169054705758.1372081021298939';
            $token = 'elbaehcfnephjgmmponpcohg.AO-J1OzR6P2_1iq6bT5rzJTarDVcsSY2zct-1Aw0II2WTzw0Dsfzrrq6GGBgFFZzlmn_Qu_9jguJA7Dkvv3QUN6rWxSzUQ_62r5UWsv2kbYbXcCHtQkTAFU';
        */

        //get access token from refresh_token
        $ch = curl_init('https://accounts.google.com/o/oauth2/token');
      //  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $post = 'grant_type=refresh_token';
        $post .= '&client_id='.GOOGLE_WALLET_CLIENTID;
        $post .= '&client_secret='.GOOGLE_WALLET_CLIENTSECRET;
        $post .= '&refresh_token='.GOOGLE_WALLET_REFRESHTOKEN;

        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

        $response = curl_exec($ch);
        $errno    = curl_errno($ch);
        $errmsg   = curl_error($ch);
        curl_close($ch);
        unset($ch);

        $ary2 = json_decode($response, true);
        if (!empty($ary2) && !empty($ary2['access_token'])) {
            $sCheckingUrl = 'https://www.googleapis.com/androidpublisher/v1.1/applications/';
            $sCheckingUrl .= GOOGLE_WALLET_PACKAGENAME . '/subscriptions/';
            $sCheckingUrl .= $productId . '/purchases/';
            $sCheckingUrl .= $purChaseToken;
            $sCheckingUrl .= '?access_token='.$ary2['access_token'];
            $ch = curl_init($sCheckingUrl);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, false);
            //curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

            $response = curl_exec($ch);
            $errno    = curl_errno($ch);
            $errmsg   = curl_error($ch);
            curl_close($ch);

            if (!empty($response)) {
                unset($ary2);
                $ary2 = json_decode($response, true);
                //var_dump($ary2);
                if (!empty($ary2) && isset($ary2['validUntilTimestampMsec'])) {
                    $epochTime = round($ary2['validUntilTimestampMsec'] / 1000, 0);
                    //echo $epochTime .'----'
                    if ($epochTime >= time()) {
                        return true;
                    }
                }
            }
        }
        return false;
     }

    /**
     * Check purchase for buy point
     * @author ThamLT
     * */
     public static function isValidInapp($productId, $purChaseToken)
     {
        /*    define('GOOGLE_WALLET_PACKAGENAME', 'com.my.firstapp');
            $subscriptionId = '12999763169054705758.1372081021298939';
            $token = 'elbaehcfnephjgmmponpcohg.AO-J1OzR6P2_1iq6bT5rzJTarDVcsSY2zct-1Aw0II2WTzw0Dsfzrrq6GGBgFFZzlmn_Qu_9jguJA7Dkvv3QUN6rWxSzUQ_62r5UWsv2kbYbXcCHtQkTAFU';
        */

        //get access token from refresh_token
        $ch = curl_init('https://accounts.google.com/o/oauth2/token');
      //  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $post = 'grant_type=refresh_token';
        $post .= '&client_id='.GOOGLE_WALLET_CLIENTID;
        $post .= '&client_secret='.GOOGLE_WALLET_CLIENTSECRET;
        $post .= '&refresh_token='.GOOGLE_WALLET_REFRESHTOKEN;

        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

        $response = curl_exec($ch);
        $errno    = curl_errno($ch);
        $errmsg   = curl_error($ch);
        curl_close($ch);
        unset($ch);

        $ary2 = json_decode($response, true);
        if (!empty($ary2) && !empty($ary2['access_token'])) {
            $sCheckingUrl = 'https://www.googleapis.com/androidpublisher/v1.1/applications/';
            $sCheckingUrl .= GOOGLE_WALLET_PACKAGENAME . '/inapp/';
            $sCheckingUrl .= $productId . '/purchases/';
            $sCheckingUrl .= $purChaseToken;
            $sCheckingUrl .= '?access_token='.$ary2['access_token'];
            $ch = curl_init($sCheckingUrl);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, false);
            //curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

            $response = curl_exec($ch);
            $errno    = curl_errno($ch);
            $errmsg   = curl_error($ch);
            curl_close($ch);

            if (!empty($response)) {
                unset($ary2);
                $ary2 = json_decode($response, true);

                if (!empty($ary2) && isset($ary2['purchaseState'])) {
                    if ($ary2['purchaseState'] == 0) {
                        return true;
                    }
                }
            }
        }
        return false;
     }
}